﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public class PdfAdminDisplay : BaseAdminDisplay<PdfContentControlProperties>
    {
        protected override string GetCssClass
        {
            get { return "pdf"; }
        }

        public override void PropertiesLoaded(PdfContentControlProperties properties)
        {
            if (properties != null)
            {
                name.Text = properties.Name;
                lnk.NavigateUrl = RenderFile.GetUrl(properties.ResourceId, Page);
            }
        }

        Label name = new Label();
        HyperLink lnk = new HyperLink();

        protected override void CreateChildControls()
        {
            this.Controls.Add(name);
            lnk.Target = "_Blank";
            lnk.Text = "Click to view PDF";
            this.Controls.Add(lnk);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if(!string.IsNullOrEmpty(lnk.NavigateUrl))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                writer.RenderBeginTag(HtmlTextWriterTag.Li);

                writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, name.ClientID));
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Name:");
                writer.RenderEndTag();//label

                name.RenderControl(writer);

                writer.RenderEndTag();//li

                writer.RenderBeginTag(HtmlTextWriterTag.Li);

                writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, lnk.ClientID));
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("PDF:");
                writer.RenderEndTag();//lable

                lnk.RenderControl(writer);

                writer.RenderEndTag();//li

                writer.RenderEndTag();//ul
            }
            else
            {
                writer.Write("No PDF has been uploaded");
            }
        }
    }
}
